from lxml import etree
from StringIO import StringIO
import os, sys

#----------------------------------------------------
##  XML-SCHEMA
f = StringIO('''\
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="a" type="AType"/>
<xsd:complexType name="AType">
  <xsd:sequence>
    <xsd:element name="b" type="xsd:string" />
  </xsd:sequence>
</xsd:complexType>
</xsd:schema>
''')

xmlschema_doc = etree.parse(f)
xmlschema = etree.XMLSchema(xmlschema_doc)

valid = StringIO('<a><b></b></a>')
doc = etree.parse(valid)
print xmlschema.validate(doc)

invalid = StringIO('<a><c></c></a>')
doc2 = etree.parse(invalid)
print xmlschema.validate(doc2)

#----------------------------------------------------
## RNG
f = StringIO('''\
<element name="canvas" xmlns="http://relaxng.org/ns/structure/1.0">
 <zeroOrMore>
    <element name="b">
      <text />
    </element>
 </zeroOrMore>
</element>
''')

relaxng_doc = etree.parse(f)
relaxng = etree.RelaxNG(relaxng_doc)

valid = StringIO('<a><b></b></a>')
doc = etree.parse(valid)
print relaxng.validate(doc)

invalid = StringIO('<a><c></c></a>')
doc2 = etree.parse(invalid)
print relaxng.validate(doc2)
